﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class zyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定数据源
            BindData();
        }
        viewIsNoData();
    }

    //绑定数据源
    protected void BindData()
    {
        //为列表视图绑定数据
        gvZY.DataSource = zy_BLL.GetSource();
        gvZY.DataBind();
        //为院系下拉列表绑定数据
        dropYX.DataSource = yx_BLL.GetSource();
        dropYX.DataTextField = "yxName";
        dropYX.DataValueField = "yxNum";
        dropYX.DataBind();
        //为科目下拉列表绑定数据        
        dropKM.DataSource = km_BLL.GetSource();
        dropKM.DataTextField = "kmName";
        dropKM.DataValueField = "kmNum";
        dropKM.DataBind();
    }
    //选择主修科目
    protected void dropKM_SelectedIndexChanged(object sender, EventArgs e)
    {
        //将已选科目添加到列表中
        if (lstSeleKM.Items.FindByText(dropKM.SelectedItem.Text) == null)
            lstSeleKM.Items.Add(new ListItem(dropKM.SelectedItem.Text, dropKM.SelectedValue));
        else
            ShowMessage("该项已存在！");
    }

    //从已选科目列表中移除选项
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            lstSeleKM.Items.RemoveAt(lstSeleKM.SelectedIndex);
        }
        catch { }
    }

    //提交
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        //验证必填信息
        if (txtZYName.Text.Length == 0 || lstSeleKM.Items.Count == 0)
        {
            ShowMessage("必要信息未填写！");
            return;
        }
        //创建专业数据对象
        ZY zy = new ZY();
        //给对象赋值
        zy.yxNum = decimal.Parse(dropYX.SelectedValue);
        zy.zyName = txtZYName.Text;
        zy.zyZF = chkZF.Checked;
        string kms = "";
        //按指定格式合成字符串
        foreach (ListItem li in lstSeleKM.Items)
        {
            kms += li.Value + "#" + li.Text + "|";
        }
        zy.zxkm = kms;

        switch (btnRefur.Text)
        {
            case "添加":
                //添加到数据库
                bool bl = zy_BLL.Add(zy);
                //显示系统消息
                if (bl)
                    //刷新当前页
                    Response.Redirect("zyPage.aspx");
                else
                {
                    ShowMessage("添加失败！");
                } break;
            case "更新":
                bool b = zy_BLL.Update(updateIndex, zy);

                //显示系统消息
                if (b)
                    //刷新当前页
                    Response.Redirect("zyPage.aspx");
                else
                {
                    ShowMessage("更新失败！");
                    btnRefur.Text = "添加";
                }
                break;
        }
    }

    //选择行
    public static string updateIndex = "";
    protected void gvZY_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //当前选择项的识别号
        updateIndex = gvZY.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选择中
        GridViewRow row = gvZY.Rows[e.NewSelectedIndex];
        //取消下拉列表中选中项
        dropYX.ClearSelection();
        //重新指定下拉列表当前项
        dropYX.Items.FindByText(row.Cells[0].Text).Selected = true;
        //获取科目字符串        
        string kms1 = row.Cells[3].Text;
        //分割字符串为“科目编号#科目名称形势”
        string[] kms2 = kms1.Split('|');
        //清空已选科目列表
        lstSeleKM.Items.Clear();
        //再次将科目编号和科目名称分割开，并添加到列表中
        foreach (string s in kms2)
        {
            try
            {
                lstSeleKM.Items.Add(new ListItem(s.Split('#')[1], s.Split('#')[0]));
            }
            catch { }
        }
        //将专业名称显示到文本框中
        txtZYName.Text = row.Cells[2].Text;
        //将专业是否作废显示在复选框中
        chkZF.Checked = row.Cells[4].Text == "是" ? true : false;
        //更改添加按钮文本
        btnRefur.Text = "更新";
    }

    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //重置下拉列表选择项
        dropYX.ClearSelection();
        dropKM.ClearSelection();
        //清空文本框数据
        txtZYName.Text = "";
        lstSeleKM.Items.Clear();
        btnRefur.Text = "添加";
        chkZF.Checked = false;
    }

    //删除数据
    protected void gvZY_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //请求删除数据
        zy_BLL.Delete(gvZY.DataKeys[e.RowIndex][0].ToString());
        ShowMessage("数据删除成功！");
        BindData();
    }
    //翻页
    protected void gvZY_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvZY.PageIndex = e.NewPageIndex;
        BindData();
    }
    #region 消息
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvZY.Rows.Count == 0)
        {
            RegisterStartupScript("prompt", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
        ////如果ListBox中没有项则隐藏该区域否则显示
        //if (lstSeleKM.Items.Count == 0)
        //{
        //    RegisterStartupScript("prompt2", "<script type='text/javascript'>$('#divSeleKM').hide()</script>");
        //}
        //else
        //{
        //    RegisterStartupScript("prompt2", "<script type='text/javascript'>$('#divSeleKM').show()</script>");
        //}
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        //注册脚本显示消息
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion

}
